<?php 
namespace Home\Controller;
use Think\Controller;

class ShanxiController extends Controller{
    private $movie_type = 2;//ott项目里2是4M_VBR
    protected function _initialize(){
        $ip = $this->get_ip();
        if($ip!='112.35.27.108'){
            exit;
        }
        $salt = 'aaaaaaaaaaaaa';
        $time = I('post.time');
        $sign = I('post.sign');
        $id = I('post.id');
        if($sign!=MD5(implode('|',[$id,$time,$salt]))){
            exit;
        }
    }

    public function getData()
    {
        $id = I('post.id');
        if(!$id){
            return;
        }
        // 获得接收剧集
        $receive_info = M('CommandQueueReceive')->where(['id'=>['gt',$id],'obj_type'=>2,'create_at'=>['lt',time()-300]])->limit(1)->find();
        if(!$receive_info){
            exit(json_encode(['result'=>'null']));
        }
        $obj_code = $receive_info['obj_code'];
        $receive_id = $receive_info['id'];
        $subset_info = M('TProgram')->where(['cp_code'=>$obj_code])->find();
        if(!$subset_info){
            exit(json_encode(['result'=>'error','id'=>$receive_info['id'],'message'=>'Incomplete subset information '.$receive_id]));
        }
        $series_info = M('TProgram')->where(['id'=>$subset_info['parent_id']])->find();
        if(!$series_info){
            exit(json_encode(['result'=>'error','id'=>$receive_info['id'],'message'=>'Incomplete series information '.$receive_id]));
        }
        $picture_data =M('PictureInfo')->alias('a')->join('join picture as b on a.id = b.picture_id')->where(['parent_id'=>$subset_info['parent_id']])->select();
        if(!$picture_data){
            exit(json_encode(['result'=>'error','id'=>$receive_info['id'],'message'=>'Incomplete picture information '.$receive_id]));
        }
        $movie_info = M('Movie')->where(['program_id'=>$subset_info['id'],'code_rate'=>$this->movie_type])->order('id desc')->limit(1)->find();
        if(!$movie_info){
            exit(json_encode(['result'=>'error','id'=>$receive_info['id'],'message'=>'Incomplete movie information '.$receive_id]));
        }
        exit(json_encode([
            'result'=>'success',
            'id'=>$receive_info['id'],
            'action'=>$receive_info['action'],
            'data'=>[
                'series'=>$series_info,
                'picture'=>$picture_data,
                'subset'=>$subset_info,
                'movie'=>$movie_info
        ]]));
    }

    private function get_ip(){
        //判断服务器是否允许$_SERVER
        if(isset($_SERVER)){    
            if(isset($_SERVER[HTTP_X_FORWARDED_FOR])){
                $realip = $_SERVER[HTTP_X_FORWARDED_FOR];
            }elseif(isset($_SERVER[HTTP_CLIENT_IP])) {
                $realip = $_SERVER[HTTP_CLIENT_IP];
            }else{
                $realip = $_SERVER[REMOTE_ADDR];
            }
        }else{
            //不允许就使用getenv获取  
            if(getenv("HTTP_X_FORWARDED_FOR")){
                  $realip = getenv( "HTTP_X_FORWARDED_FOR");
            }elseif(getenv("HTTP_CLIENT_IP")) {
                  $realip = getenv("HTTP_CLIENT_IP");
            }else{
                  $realip = getenv("REMOTE_ADDR");
            }
        }
        return $realip;
    }
}